package 力扣.二分;

public class 长度最小的子数组209 {
    public int minSubArrayLen(int target, int[] nums) {
         if (nums == null || nums.length == 0 || target == 0){
             return 0;
         }
         int N = nums.length;
         int l = 1, r = N + 1;
         while (l < r){
             int m = l + ((r - l) >> 1);
             int mov = getC(nums,m,target);
             if (mov < 0){
                 l = m + 1;
             }else {
                 r = m;
             }
         }
         return l <= N ? l : 0;
    }

    private int getC(int[] nums, int len,int target) {
        int cnt = 0;
        int left = 0;
        for (int i = 0; i < nums.length; i++) {
            int te = nums[i];
            cnt += te;
            if (i < len - 1){
                continue;
            }
            if (cnt >= target){
                return 0;
            }
            cnt -= nums[left];
            left++;
        }
        return -1;
    }
}
